{extend name="main" /}

{block name="title_seo"}
<title>注册</title>
<meta name="keywords" charset="UTF-8" content="">
<meta name="description" content="">
{/block}

{block name="body"}
<div class="layui-container">
    <div class="layui-row layui-col-space30" style="padding: 150px 0;text-align: center; margin-top: 60px;" >
        <style>
            .demo-login-container{width: 320px; margin: 21px auto 0;}
            .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
        </style>
        <form class="layui-form">
            <div class="demo-login-container">
                {if condition="xn_cfg('login.account_type') eq 'mobile'"}
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-cellphone"></i>
                            </div>
                            <input type="text" name="mobile" id="mobile" value="" lay-verify="required|phone" placeholder="手机号"
                                   lay-reqtext="请填写手机号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    {if condition="xn_cfg('login.ver_mobile') eq '1'"}
                    <div class="layui-form-item">
                        <div class="layui-row">
                            <div class="layui-col-xs7">
                                <div class="layui-input-wrap">
                                    <div class="layui-input-prefix">
                                        <i class="layui-icon layui-icon-vercode"></i>
                                    </div>
                                    <input type="text" name="code" value="" lay-verify="required" placeholder="验证码"
                                           lay-reqtext="请填写验证码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-xs5">
                                <div style="margin-left: 11px;">
                                    <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary" id="get-vercode" onclick="sendSmsCode()">
                                        获取验证码
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    {/if}
                {elseif condition="xn_cfg('login.account_type') eq 'email'"}
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-email"></i>
                            </div>
                            <input type="text" name="email" value="" lay-verify="required" placeholder="邮箱地址" lay-reqtext="请填写邮箱地址" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                {else /}
                    <div class="layui-form-item">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-username"></i>
                            </div>
                            <input type="text" name="username" value="" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                {/if}
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-friends"></i>
                        </div>
                        <input type="text" name="nickname" value="" lay-verify="required" placeholder="昵称" lay-reqtext="请填写昵称" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="password" value="" lay-verify="required" placeholder="密码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="repassword" value="" lay-verify="required" placeholder="重复密码" lay-reqtext="重复密码" autocomplete="off" class="layui-input" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" name="captcha" value="" lay-verify="required" placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <img src="{:url('login/captcha')}" class="" style="max-height: 40px" id="captcha">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login">注册</button>
                </div>
                <div class="layui-form-item demo-login-other">
                    <a href="{:url('login')}">已有帐号</a>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use(function(){
        var form = layui.form;
        var layer = layui.layer;
        // 提交事件
        form.on('submit(demo-login)', function(data){
            var field = data.field; // 获取表单字段值
            var url = "{:url('login/register')}";
            $.post(url,field,function (res) {
                if ( res.code === 1 ) {
                    layer.msg(res.msg,{icon:1,time:1500},function () {
                        window.location.href = "{:url('login/login')}";
                    });
                } else {
                    $('#captcha').attr('src', "{:url('login/captcha')}?" + Date.parse(new Date()))
                    layer.msg(res.msg,{icon:2,time:1500});
                }
            },'json')

            return false;
        });
    });

    $('#captcha').click(function () {
        $(this).attr('src', "{:url('login/captcha')}?" + Date.parse(new Date()))
    })

    //发送短信验证码
    function sendSmsCode() {
        var mobile = $('#mobile').val();
        var url = "{:url('smsRegCode')}";
        $.post(url, {mobile: mobile}, function (data) {
            if (data.code === 1) {
                doLoop(60)
            } else {
                alert(data.msg);
            }
        }, 'json')
    }

    // 倒计时
    function doLoop(seconds) {
        seconds = seconds ? seconds : 60;
        $('#get-vercode').html(seconds + "s后获取");
        $('#get-vercode').attr('disabled', true);
        $('#get-vercode').addClass('layui-btn-disabled');
        let countdown = setInterval(() => {
            if (seconds > 0) {
                $('#get-vercode').html(seconds + "s后获取")
                --seconds;
            } else {
                $('#get-vercode').html("获取验证码");
                $('#get-vercode').attr('disabled', false);
                $('#get-vercode').removeClass('layui-btn-disabled');
                clearInterval(countdown);
            }
        }, 1000);
    }
</script>
{/block}